part of 'alert.dart';

class _AlertRoute extends PopupRoute {
  final Widget child;

  _AlertRoute({required this.child});

  @override
  Color get barrierColor => Color(0xff979797).withOpacity(0.4);

  @override
  bool get barrierDismissible => false;

  @override
  String get barrierLabel => '';

  @override
  Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
    return MediaQuery.removePadding(
      context: context,
      removeTop: true,
      child: child,
    );
  }

  @override
  Duration get transitionDuration => Duration(milliseconds: 200);
}

class _IAlert extends StatefulWidget {
  final ContentType contentType;
  final ActionType actionType;
  final ConfirmType confirmType;
  final String? title;
  final String? content;
  final ContentBuilder? builder;
  final Color? confirmTextColor;
  final Color? cancelTextColor;
  final String? confirmTitle;
  final String? cancelTitle;
  final OnCancel? onCancel;
  final OnConfirm? onConfirm;
  final bool inWindowCenter;
  final bool needScroll;
  final bool canPop;

  const _IAlert(
      {super.key,
      required this.contentType,
      required this.actionType,
      required this.confirmType,
      this.title,
      this.content,
      this.builder,
      this.confirmTextColor,
      this.cancelTextColor,
      this.confirmTitle,
      this.cancelTitle,
      this.onCancel,
      this.onConfirm,
      this.needScroll = false,
      this.inWindowCenter = false,
      this.canPop = true});

  @override
  State<StatefulWidget> createState() {
    return _IAlertState();
  }
}

class _IAlertState extends State<_IAlert> with WidgetsBindingObserver {
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        return widget.canPop;
      },
      child: Scaffold(
        backgroundColor: Colors.transparent,

        ///避免底部被弹上来
        resizeToAvoidBottomInset: false,
        body: Column(
          children: [
            Expanded(
              child: Container(),
              flex: 275,
            ),
            _content(),
            SizedBox(
              height: 40,
            ),
            Expanded(
              child: Container(),
              flex: 275,
            )
          ],
        ),
      ),
    );
  }

  _content() {
    return Container(
      width: 280.rpx,
      decoration: BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(16)), color: Colors.white),
      child: ClipRRect(
        borderRadius: BorderRadius.all(Radius.circular(16)),
        child: Column(
          children: [_layoutTitle(), widget.needScroll ? _layout_needScrool() : _layoutContent(), _layoutBtn()],
        ),
      ),
    );
  }

  _cancelBtn() {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      child: Center(
        child: Text(
          '${widget.cancelTitle}',
          style: TextStyle(fontSize: 16.rpx, color: widget.cancelTextColor),
        ),
      ),
      onTap: () {
        Navigator.of(context).pop();
        widget.onCancel?.call();
      },
    );
  }

  _confirm() {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      child: Center(
        child: Text(
          '${widget.confirmTitle}',
          style: TextStyle(fontSize: 16.rpx, color: widget.confirmTextColor),
        ),
      ),
      onTap: () async {
        if (widget.confirmType == ConfirmType.VERIFY) {
          bool result = await widget.onConfirm?.call() ?? true;
          if (result) {
            Navigator.of(context).pop( true);
          }
        } else {
          Navigator.of(context).pop(true);
          widget.onConfirm?.call();
        }
      },
    );
  }

  _centralBtn() {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      child: Center(
        child: Center(
          child: Text(
            '${widget.confirmTitle}',
            style: TextStyle(fontSize: 16.rpx, color: widget.confirmTextColor),
          ),
        ),
      ),
      onTap: () async{
        if (widget.confirmType == ConfirmType.VERIFY) {
          bool result = await widget.onConfirm?.call() ?? true;
          if (result) {
            Navigator.of(context).pop( true);
          }
        } else {
          Navigator.of(context).pop(true);
          widget.onConfirm?.call();
        }
      },
    );
  }

  _layoutBtn() {
    if (widget.actionType == ActionType.NONE) {
      return Container();
    }
    if (widget.actionType == ActionType.SIMPLE) {
      return Container(
        height: 50.rpx,
        decoration: BoxDecoration(
            border: Border(
          top: BorderSide(color: Color(0xff000000).withOpacity(0.1), width: 0.5.rpx),
        )),
        child: Row(
          children: [
            Expanded(
              child: _cancelBtn(),
            ),
            Container(
              width: 0.5.rpx,
              color: Color(0xff000000).withOpacity(0.1),
            ),
            Expanded(
              child: _confirm(),
            ),
          ],
        ),
      );
    }
    return Container(
      height: 52.rpx,
      decoration: BoxDecoration(
          border: Border(
        top: BorderSide(color: Color(0xff000000).withOpacity(0.1), width: 0.5.rpx),
      )),
      child: Row(
        children: [
          Expanded(
            child: _centralBtn(),
          ),
        ],
      ),
    );
  }

  _layoutTitle() {
    if (widget.contentType == ContentType.SIMPLE) {
      return Padding(
        padding: EdgeInsets.only(top: 30.rpx),
        child: Text(
          '${widget.title}',
          style: TextStyle(fontSize: 16.rpx, color: Color(0xff333333), fontWeight: FontWeight.w500, height: 1.0),
        ),
      );
    }
    return Container();
  }

  _layout_needScrool() {
    if (widget.contentType == ContentType.NO_TITLE) {
      return Container(
        constraints: BoxConstraints(maxHeight: 500),
        padding: EdgeInsets.symmetric(horizontal: 34.rpx).copyWith(top: 21.5.rpx, bottom: 26.5.rpx),
        child: SingleChildScrollView(
          child: Column(
            children: [
              Container(
                color: Colors.transparent,
                width: double.maxFinite,
                child: Text(
                  '${widget.content}',
                  style: TextStyle(fontSize: 15.rpx, color: Color(0xff333333)),
                ),
              )
            ],
          ),
        ),
      );
    }
  }

  _layoutContent() {
    if (widget.contentType == ContentType.NO_TITLE) {
      return Padding(
        padding: EdgeInsets.symmetric(horizontal: 34.rpx),
        child: Column(
          children: [
            Padding(
              padding: EdgeInsets.only(top: 21.5.rpx),
            ),
            Text(
              '${widget.content}',
              style: TextStyle(fontSize: 15.rpx, color: Color(0xff333333)),
            ),
            Padding(padding: EdgeInsets.only(bottom: 26.5.rpx)),
          ],
        ),
      );
    }

    if (widget.contentType == ContentType.CUSTOM) {
      assert(widget.builder != null);
      return widget.builder!(context);
    }

    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 34.rpx),
      child: Column(
        children: [
          Padding(
            padding: EdgeInsets.only(top: 11.5.rpx),
          ),
          Text(
            '${widget.content}',
            style: TextStyle(fontSize: 15.rpx, color: Color(0xff999999)),
          ),
          Padding(padding: EdgeInsets.only(bottom: 25.5.rpx)),
        ],
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeMetrics() {
    super.didChangeMetrics();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      double h = MediaQueryData.fromWindow(window).viewInsets.bottom; //键盘的高度
    });
  }
}
